package cn.hutool.postfix.generator;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.text.StrPool;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.postfix.generator.common.Config;

import java.io.File;
import java.util.List;
import java.util.Optional;

/**
 * @author liuxiongkang
 */
public class RuleCounter {

    private static final Log LOG = LogFactory.get(RuleCounter.class);

    private RuleCounter() {
        //do nothing
    }

    /**
     * 单独统计 条数
     */
    public static void main(String[] args) {
        countHutoolAll();
    }

    protected static void countHutoolAll() {
        Config.LANG.forEach(lang -> countHutoolAll(lang.lang(), FileUtil.file(RuleDocletGenerator.TEMP_PATH + StrPool.C_SLASH + lang.lang())));
    }

    private static void countHutoolAll(String lang, File file) {
        if (!file.exists() || !file.isDirectory()) {
            return;
        }
        Optional<File> all = FileUtil.loopFiles(file, f -> f.getName().contains("hutool-all")).stream().findFirst();
        all.ifPresent(f -> {
            Pair<Long, Long> count = countHutoolAll(f);
            LOG.info("========== lang:{}, 方法数:{}, 规则总数:{} ==========", lang, count.getKey(), count.getValue());
        });
    }

    private static Pair<Long, Long> countHutoolAll(File file) {
        if (!file.exists()) {
            return Pair.of(0L, 0L);
        }
        List<String> filterLine = RuleOverrides.geRuleList(file);
        long methodCount = filterLine.stream().filter(line -> line.startsWith(".")).count();
        long ruleCount = filterLine.stream().filter(line -> line.contains("→")).count();
        return Pair.of(methodCount, ruleCount);
    }
}
